package com.hzy.alibaba.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hzy.alibaba.entity.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/***
 * @author abc
 * @desc TController
 * @date  2019/11/26 0026 下午 14:59
 */
@SuppressWarnings("ALL")
public class BaseController<Service extends IService<T>, T> {
    @Autowired
    private Service service;

    /**
     * 分页
     * @param t
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping("page")
    public IPage<T> getList(@ModelAttribute T t, @ModelAttribute Page<T> page) throws Exception {
        return service.page(page, new QueryWrapper(t));
    }

    /**
     * 新增
     * @param t
     * @throws Exception
     */
    @PostMapping("add")
    @ResponseBody
    public R add(@RequestBody T t) throws Exception {
        service.save(t);
        return R.ok();
    }

    /**
     * 详情
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/get/{id}")
    public T get(@PathVariable String id) throws Exception {
        return service.getById(id);
    }

    /**
     * 修改
     * @param t
     * @throws Exception
     */
    @PutMapping("modify")
    @ResponseBody
    public R modify(@RequestBody T t) throws Exception {
        service.updateById(t);
        return R.ok();
    }

    /**
     * 删除
     * @param id
     * @throws Exception
     */
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable String id) throws Exception {
        service.removeById(id);
        return R.ok();
    }
}
